VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "ClsSucursalFacturacion"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit

Public WithEvents orset As ClsRecordset
Attribute orset.VB_VarHelpID = -1

Public Event DataChanged()

Public codigo As String
Public numero As Integer
Public nombre As String
Public direccion As String

' Class event handlers
Private Sub Class_Initialize()
    Set orset = New ClsRecordset
    orset.setCampos dbCampos(sfa)
    orset.setTablas Array(sfa)
End Sub
Private Sub Class_Terminate()
    Set orset = Nothing
End Sub
' Generic Data management methods
Public Sub consulta()
    orset.consulta
End Sub
Public Sub consultaDatos(codig As String, Optional optnum As String)
    If optnum = "AL" Then
        orset.consulta Array(sfa_nombre & " LIKE '" & optnum & "%'")
    ElseIf optnum <> "" Then
        orset.consulta Array(sfa_numero & "=" & optnum)
    Else
        orset.consulta Array(sfa_id & "=" & codig)
    End If
    orset.CierraRec
End Sub

Private Sub Reload_members()
    With orset
        codigo = .getDato(sfa_id)
        numero = .getDato(sfa_numero)
        nombre = .getDato(sfa_nombre)
        direccion = .getDato(sfa_direccion)
    End With
    RaiseEvent DataChanged
End Sub
Public Sub Nuevo(newNumero As String, newNombre As String, newDireccion As String)
    orset.sInsert Array("", newNumero, newNombre, newDireccion)
End Sub
Public Sub Modificar(codig As String, newNumero As String, newNombre As String, newDireccion As String)
    orset.sUpdate Array(codig, newNumero, newNombre, newDireccion)
End Sub
Public Sub Borrar(codig As String)
    orset.sDelete Array(sfa_id & "=" & codig)
End Sub
' Class methods
Public Sub llenaCombo(cajaCombo As ComboBox, conCodigo As Boolean, Optional conTodos As Boolean = False, Optional conNumero As Boolean = False, Optional codSeleccionado As String)
    Dim i As Integer
    Me.consulta
    i = 0
    cajaCombo.Clear
    If conCodigo Then
        If conTodos Then
            cajaCombo.AddItem "Todos"
            cajaCombo.ItemData(i) = 0
            i = i + 1
        End If
        While orset.CanMoveNext
            cajaCombo.AddItem codigo & " - " & nombre
            cajaCombo.ItemData(i) = codigo
            If codigo = codSeleccionado Then
                cajaCombo.ListIndex = i
            End If
            orset.MoveNext
            i = i + 1
        Wend
    Else
        If conTodos Then
            cajaCombo.AddItem "Todos"
            cajaCombo.ItemData(i) = 0
            i = i + 1
        End If
        While orset.CanMoveNext
            If conNumero Then
                cajaCombo.AddItem numero & " - " & nombre
            Else
                cajaCombo.AddItem nombre
            End If
            cajaCombo.ItemData(i) = codigo
            If codigo = codSeleccionado Then
                cajaCombo.ListIndex = i
            End If
            orset.MoveNext
            i = i + 1
        Wend
    End If
    If cajaCombo.ListCount = 1 And cajaCombo.ListIndex = -1 Then
        cajaCombo.ListIndex = 0
    End If
End Sub
Public Sub llenaList(cajaList As ListBox, conNumero As Boolean, conCodigo As Boolean)
    Dim i As Integer
    Me.consulta 'optHabil
    i = 0
    cajaList.Clear
    If conCodigo Then
        While orset.CanMoveNext
            cajaList.AddItem arreglanum2dig(Me.codigo) & " - " & nombre
            cajaList.ItemData(i) = codigo
            orset.MoveNext
            i = i + 1
        Wend
    Else
        If conNumero Then
            While orset.CanMoveNext
                cajaList.AddItem arreglanum2dig(Me.numero) & " - " & nombre
                cajaList.ItemData(i) = codigo
                orset.MoveNext
                i = i + 1
            Wend
        Else
            While orset.CanMoveNext
                cajaList.AddItem nombre
                cajaList.ItemData(i) = codigo
                orset.MoveNext
                i = i + 1
            Wend
        End If
    End If
    'If cajaList.ListCount = 1 Then
    '    cajaList.ListIndex = 0
    'End If
    orset.CierraRec
End Sub
Public Sub llenaHGrid(Grid As MSHFlexGrid, Optional anchos As Variant, Optional titulos As Variant)
    Dim i As Integer
    With Grid
    'vacia el grid
        .Row = 0
        .Clear
    'setea numero de columnas y filas
        .Cols = 4
        .Rows = 2
    'coloca datos
        Me.consulta
        If orset.numRegistros > 0 Then
            Set .DataSource = orset.mrec
        Else
            .Rows = 2
        End If
        orset.CierraRec
    'setea anchos
        If IsMissing(anchos) Then
            .ColWidth(0) = 40 * 15
            .ColWidth(1) = 40 * 15
            .ColWidth(2) = 75 * 15
            .ColWidth(3) = 150 * 15
        Else
            'jala anchos de la variable
            cambiaAnchosHFG Grid, anchos
        End If
    'coloca titulos
        If IsMissing(titulos) Then
            .TextMatrix(0, 0) = "Codigo"
            .TextMatrix(0, 1) = "Numero"
            .TextMatrix(0, 2) = "Nombre"
            .TextMatrix(0, 3) = "Direccion"
        Else
            'jala titulos de la variable
            cambiaTitulosHFG Grid, titulos
        End If
        .Refresh
        .Row = 1
        .Col = 0
    End With
End Sub

Private Sub orset_DataChanged()
    Reload_members
End Sub

